package cn.edu.lzu.userweb.controller;

import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.entity.ExaminePackage;
import cn.edu.lzu.entity.Unit;
import cn.edu.lzu.entity.User;
import cn.edu.lzu.service.ExaminePackageService;
import cn.edu.lzu.service.UnitService;
import cn.edu.lzu.service.UserService;
import com.fasterxml.jackson.databind.Module;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.List;

/**
 * @PackageName: cn.edu.lzu.userweb.controller
 * @ClassName: UnitController
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2020/5/17 9:46
 * @Version: 1.0
 */
@Controller
public class UnitController {
    @Resource
    private UnitService unitService;
    @Resource
    private ExaminePackageService examinePackageService;
    @Resource
    private UserService userService;

    @ResponseBody
    @RequestMapping("/getUnitsByProvinceId")
    public List<Unit> getUnitsByProvinceId(Long provinceId) {
        List<Unit> unitList = unitService.getUnitsByProvinceId(provinceId);
        return unitList;
    }

    @ResponseBody
    @RequestMapping("/getUnitsByAreaId")
    public List<Unit> getUnitsByAreaId(Long areaId) {
        List<Unit> unitList = unitService.getUnitsByAreaId(areaId);
        return unitList;
    }

    @ResponseBody
    @RequestMapping("/getUnitsByCityId")
    public List<Unit> getUnitsByCityId(Long cityId) {
        List<Unit> unitList = unitService.getUnitsByCityId(cityId);
        return unitList;
    }

    @RequestMapping("/xiangqing")
    public String xiangqing(Long unitId, HttpServletRequest request) {
        request.setAttribute("unitId", unitId);
        return "xiangqing.html";
    }

    @ResponseBody
    @RequestMapping("/getUnitPackages")
    public List<ExaminePackage> getUnitPackages(Long unitId){
        System.out.println("unitId = " + unitId);
        List<ExaminePackage> packageList=examinePackageService.getUnitPackages(unitId);
        return packageList;
    }

    @RequestMapping("/mingxi")
    public String mingxi(Integer examinePackageId, Long unitId, HttpServletRequest request){
        request.setAttribute("examinePackageId", examinePackageId);
        request.setAttribute("unitId", unitId);
        System.out.println("examinePackageId = " + examinePackageId + ", unitId = " + unitId);
        return "mingxi.html";
    }
    @ResponseBody
    @RequestMapping("/userLogin")
    public ResponseResult userLogin(String userIdentity, String password, HttpServletRequest request){
        System.out.println("userIdentity = " + userIdentity + ", password = " + password);
        Subject subject= SecurityUtils.getSubject();
        ResponseResult responseResult=new ResponseResult();
        UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(userIdentity, password);
        try{
            subject.login(usernamePasswordToken);
            responseResult.setCode(1);
            responseResult.setMsg("登录成功");
            request.getSession().setAttribute("login", true);
            return responseResult;
        }catch (Exception e){
            System.out.println(e.getLocalizedMessage());
            responseResult.setCode(0);
            responseResult.setMsg("登录失败"+e.getLocalizedMessage());
            return responseResult;
        }
    }

    @ResponseBody
    @RequestMapping("/bookExamine")
    public ResponseResult yuyue(Integer examinePackageId, Date examineDate, Long unitId){
        System.out.println("examinePackageId = " + examinePackageId + ", examineDate = " + examineDate + ", unitId = " + unitId);
        Session session=SecurityUtils.getSubject().getSession();
        User user=(User)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        ResponseResult responseResult=userService.bookSubmitUserMsg(user, unitId, examinePackageId, examineDate);
        return responseResult;
    }

    @ResponseBody
    @RequestMapping("/logout")
    public boolean logout(HttpServletRequest request){
        Subject subject = SecurityUtils.getSubject();
        try {
            request.getSession().setAttribute("login", false);
            subject.logout();
            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }



}
